package com.wdj.library.service.impl;

import com.wdj.library.service.MyWebService;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.springframework.stereotype.Service;

@Service("myWebService")
public class MyWebServiceImpl implements MyWebService {
    @Override
    public String webService() {
        return client();
    }

    /**
     * 2：动态调用
     */
    private String client() {
        // 创建动态客户端
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient("http://localhost:8080/demo/api?wsdl");
        // 需要密码的情况需要加上用户名和密码
        // client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME, PASS_WORD));
        Object[] objects = new Object[0];
        try {
            // invoke("方法名",参数1,参数2,参数3....);
            objects = client.invoke("sayHello", "张三");
            return objects[0].toString();
        } catch (java.lang.Exception e) {
            e.printStackTrace();
        }
        return "调用webService失败喔";
    }
}
